package cn.tedu.ivos.application.sevice.impl;

import cn.tedu.ivos.application.mapper.ApplicationMapper;
import cn.tedu.ivos.application.pojo.dto.ApplicationQuery;
import cn.tedu.ivos.application.pojo.dto.ApplicationSaveParam;
import cn.tedu.ivos.application.pojo.entity.Application;
import cn.tedu.ivos.application.pojo.vo.ApplicationVO;
import cn.tedu.ivos.application.sevice.ApplicationService;
import cn.tedu.ivos.audit.pojo.vo.AuditVO;
import cn.tedu.ivos.audit.service.AuditService;
import cn.tedu.ivos.base.enums.ApplicationStatusEnum;
import cn.tedu.ivos.user.mapper.UserMapper;
import cn.tedu.ivos.user.pojo.vo.UserVO;
import cn.tedu.ivos.vehicle.mapper.VehicleMapper;
import cn.tedu.ivos.vehicle.pojo.entity.Vehicle;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/*
* @Transactional是Spring提供的事务注解,用来保证业务操作要么全部成功,要么全部失败
* */
@Transactional
@Slf4j
@Service
public class ApplicationServiceImpl implements ApplicationService {
    @Autowired
    ApplicationMapper applicationMapper;
    @Autowired
    AuditService auditService;
    @Autowired
    UserMapper userMapper;
    @Autowired
    VehicleMapper vehicleMapper;

    @Override
    public void saveApplication(ApplicationSaveParam applicationSaveParam) {
        log.debug("新增申请单业务，参数：{}", applicationSaveParam);
        Application application = new Application();
        BeanUtils.copyProperties(applicationSaveParam, application);
        application.setStatus(ApplicationStatusEnum.PENGDING.getCode());
        application.setCreateTime(new Date());
        /*遇到的问题:新增申请单对应的审批单时,审批单中没有此申请单id
         * 原因:执行insert方法的SQL时,并没有把刚刚生成的id回填到application对象中
         * 解决办法:给此SQL上加userGeneratedKets="true" keyProperty="id"属性
         * 效果:JDBC自动回填此申请单id到application对象的id属性中,再传给审批,
         * 审批就拿到申请单id了*/
        applicationMapper.insert(application);

        //除了要新增申请单,还要根据此申请单生成对应的审批单单
        auditService.insertAudit(application);
    }

    @Override
    public List<ApplicationVO> selectApplication(ApplicationQuery applicationQuery) {
        log.debug("查询申请单列表业务，参数：{}", applicationQuery);
        List<ApplicationVO> list = applicationMapper.selectApplication(applicationQuery);
        //遍历查出来的每一个申请单VO
        for (int i = 0; i < list.size(); i++) {
            ApplicationVO applicationVO = list.get(i);
            //依次为每个遍历到的申请单VO补全审批人数据
            assignAuditUserList(applicationVO);
        }
        return list;
    }

    @Override
    public void cancelApplication(Long id) {
        log.debug("撤销申请单业务，参数：{}", id);
        Application application = new Application();
        application.setId(id);
        application.setStatus(ApplicationStatusEnum.CANCEL.getCode());
        application.setUpdateTime(new Date());
        applicationMapper.cancelById(application);
        auditService.deletAudit(id);
    }

    @Override
    public void distribute(Long applicationId, Long vehicleId) {
        log.debug("车辆分配业务，参数：{},{}", applicationId, vehicleId);
        Application application = new Application();
        application.setId(applicationId);
        application.setStatus(ApplicationStatusEnum.ALLOCATION.getCode());
        application.setVehicleId(vehicleId);
        application.setUpdateTime(new Date());
        applicationMapper.cancelById(application);

        Vehicle vehicle = new Vehicle();
        vehicle.setId(vehicleId);
        vehicle.setStatus("2");
        vehicle.setUpdateTime(new Date());
        vehicleMapper.updateVehicle(vehicle);
    }

    private void assignAuditUserList(ApplicationVO applicationVO) {
        //1.准备空集合,分别用来保存审批人id与姓名
        List<Long> auditUserIdList = new ArrayList<>();
        List<String> auditUsernameList = new ArrayList<>();
        //2.根据传入的申请单对象,获取其id,查出此申请单对应的审批单
        List<AuditVO> auditVOList =
                auditService.selectAuditByApplicationId(applicationVO.getId());
        //3.依次遍历取出每一个审批单对象
        for(int i = 0; i < auditVOList.size(); i++){
            AuditVO auditVO = auditVOList.get(i);
            //4.获取当前审批单的审批人数据
            Long id = auditVO.getAuditUserId();
            auditUserIdList.add(id);
            //5.获取当前审批人的姓名
            UserVO userVO = userMapper.selectById(id);
            auditUsernameList.add(userVO.getUsername());
        }
        //6.准备一个拼接工具,将多个审批人姓名连成一个字符串
        StringJoiner stringJoiner = new StringJoiner(",");
        for(String username : auditUsernameList){
            stringJoiner.add(username);
        }
        //7.将准备好的数据赋值给传入的ApplicationVO
        applicationVO.setAuditUserIdList(auditUserIdList);
        applicationVO.setAuditUsernameList(stringJoiner.toString());
    }
}
